package org.jeecg.modules.wangyue.controller.sys;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.wangyue.constant.DelFlagConstant;
import org.jeecg.modules.wangyue.entity.IndentJs;
import org.jeecg.modules.wangyue.entity.ServiceItems;
import org.jeecg.modules.wangyue.entity.ServiceType;
import org.jeecg.modules.wangyue.model.req.ReqCommonModel;
import org.jeecg.modules.wangyue.service.IServiceItemsService;
import java.util.Date;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.wangyue.service.IServiceTypeService;
import org.jeecg.modules.wangyue.utils.CommonUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 服务项目管理
 * @Author: jeecg-boot
 * @Date:   2023-06-30
 * @Version: V1.0
 */
@Slf4j
@Api(tags="服务项目管理")
@RestController
@RequestMapping("/wangyue/sys/serviceItemsManage")
@Validated
public class ServiceItemsManageController extends JeecgController<ServiceItems, IServiceItemsService> {
	@Autowired
	private IServiceItemsService serviceItemsService;
	@Autowired
	private IServiceTypeService serviceTypeService;

	 /**
	  * 服务项目管理-分页列表查询
	  *
	  * @param keywords			关键字
	  * @param serviceTypeCode	服务类型
	  * @return
	  */
	@AutoLog(value = "服务项目管理-分页列表查询")
	@ApiOperation(value="服务项目管理-分页列表查询", notes="服务项目管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(@RequestParam(name="keywords", required = false) String keywords,
								   @RequestParam(name="serviceTypeCode", required = false) String serviceTypeCode,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		QueryWrapper<ServiceItems> queryWrapper = new QueryWrapper<>();
		if(oConvertUtils.isNotEmpty(keywords)){
			queryWrapper.like("service_items_name", keywords);
		}
		if(oConvertUtils.isNotEmpty(serviceTypeCode)){
			queryWrapper.eq("service_type_code", serviceTypeCode);
		}
		queryWrapper.eq("del_flag", DelFlagConstant.normal);
		queryWrapper.orderByDesc("create_time");
		Page<ServiceItems> page = new Page<ServiceItems>(pageNo, pageSize);
		IPage<ServiceItems> pageList = serviceItemsService.page(page, queryWrapper);
		return Result.OK(pageList);
	}


	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "服务项目管理-通过id查询")
	@ApiOperation(value="服务项目管理-通过id查询", notes="服务项目管理-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) @NotBlank(message = "id不能为空") String id) {
	 	QueryWrapper<ServiceItems> queryWrapper = new QueryWrapper<>();
	 	queryWrapper.eq("id", id);
		queryWrapper.eq("del_flag", DelFlagConstant.normal);
		ServiceItems serviceItems = serviceItemsService.getOne(queryWrapper);
	 	return Result.OK(serviceItems);
	}


	 /**
	  * 服务项目管理-查询服务类型
	  *
	  * @return
	  */
	 @AutoLog(value = "服务项目管理-查询服务类型")
	 @ApiOperation(value="服务项目管理-查询服务类型", notes="服务项目管理-查询服务类型")
	 @GetMapping(value = "/queryServcieType")
	 public Result<?> queryServcieType() {
		 QueryWrapper<ServiceType> queryWrapper = new QueryWrapper<>();
		 queryWrapper.eq("del_flag", DelFlagConstant.normal);
		 List<ServiceType> serviceTypeList = serviceTypeService.list(queryWrapper);
		 return Result.OK(serviceTypeList);
	 }


	 /**
	  * 服务项目管理-上下架
	  *
	  * @param serviceItems
	  * @return
	  */
	 @AutoLog(value = "服务项目管理-上下架")
	 @ApiOperation(value="服务项目管理-上下架", notes="服务项目管理-上下架")
	 @PostMapping(value = "/editLive")
	 public Result<?> editLive(@RequestBody ServiceItems serviceItems) {
		 UpdateWrapper<ServiceItems> updateWrapper = new UpdateWrapper<>();
		 updateWrapper.eq("id", serviceItems.getId());
		 updateWrapper.eq("del_flag", DelFlagConstant.normal);
		 updateWrapper.set("is_live", serviceItems.getIsLive());
		 if(serviceItemsService.update(updateWrapper)){
		 	return Result.OK("操作成功");
		 }
		 return Result.error("操作失败");
	 }


	/**
	 * 服务项目管理-添加或修改
	 *
	 * @param serviceItems
	 * @return
	 */
	@AutoLog(value = "服务项目管理-添加或修改")
	@ApiOperation(value="服务项目管理-添加或修改", notes="服务项目管理-添加或修改")
	@PostMapping(value = "/addOrEdit")
	public Result<?> addOrEdit(@Valid @RequestBody ServiceItems serviceItems) {
		if(oConvertUtils.isEmpty(serviceItems.getId())){ //如果是新增
			serviceItems.setServiceItemsCode(CommonUtil.getOrderNum()); //随机生产一个唯一code
		}
		if(serviceItemsService.saveOrUpdate(serviceItems)){
			return Result.OK("添加或修改成功！");
		}
		return Result.error("添加或修改失败！");
	}


	/**
	 * 通过id删除
	 *
	 * @param reqCommonModel
	 * @return
	 */
	@AutoLog(value = "服务项目管理-通过id删除")
	@ApiOperation(value="服务项目管理-通过id删除", notes="服务项目管理-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@Valid @RequestBody ReqCommonModel reqCommonModel) {
		if(serviceItemsService.removeById(reqCommonModel.getId())){
			return Result.OK("删除成功!");
		}
		return Result.error("删除失败!");
	}


	/**
	 * 批量删除
	 *
	 * @param reqCommonModel  (id存放多个数据的id，用逗号隔开)
	 * @return
	 */
	@AutoLog(value = "服务项目管理-批量删除")
	@ApiOperation(value="服务项目管理-批量删除", notes="服务项目管理-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@Valid @RequestBody ReqCommonModel reqCommonModel) {
		if(this.serviceItemsService.removeByIds(Arrays.asList(reqCommonModel.getId().split(",")))){
			return Result.OK("批量删除成功！");
		}
		return Result.error("批量删除失败！");
	}
	



}
